package component

import (
	"gitee.com/fierce_wolf/go-fox-edge-common/commEnv"
	"gitee.com/fierce_wolf/go-fox-edge-common/commUtil/FileName"
	"gitee.com/fierce_wolf/go-fox-edge-common/commUtil/periodTask"
	"os"
	"strings"
	"time"
)

type cleanLogFileTask struct {
	statusMap map[string]interface{}
}

func (e *cleanLogFileTask) TaskType() periodTask.TaskType {
	return periodTask.TaskShare
}

func (e *cleanLogFileTask) SchedulePeriod() int64 {
	return 3600
}

func (e *cleanLogFileTask) Execute() {
	filePath := FileName.GetOsFilePath(commEnv.Service.WorkDir + "/logs")

	fileList := FileName.ListFile(filePath, false, true)
	for _, fileName := range fileList {
		// 检查：是否为.gz文件，这文件是日志压缩文件
		if !strings.HasSuffix(fileName, ".gz") {
			continue
		}

		fs, err := os.Stat(fileName)
		if err != nil {
			continue
		}

		// 检查：是否为过期的文件
		timeout := time.Now().UnixMilli() - fs.ModTime().UnixMilli()
		if timeout < 7*24*3600*1000 {
			continue
		}

		// 删除过期的文件
		os.Remove(fileName)

	}

}
